// file: 	cs-zip.do
// purpose:	merge Pew surveys with geographic identifiers at multiple levels (from zip);
//			create party match variables using SLER data at state legislative levels;
//			estimate models and make Tables 0A9-12, Fig. OA 3.

use cs-recode.dta, clear
tab opst


* Only 2005 and 2008 surveys contain zip codes
keep if year==2005 | year==2008
gen zcta5 = zipcode

* merge with geographic correspondence codes from Missouri Census Data Center (cleaned)
joinby zcta5 using zip_county.dta, unmatched(master) _merge(mgzip_county)
joinby zcta5 using zip_puma.dta, unmatched(master) _merge(mgzip_puma)
joinby zcta5 using zip_sldl.dta, unmatched(master) _merge(mgzip_sldl)
tab opst
replace sldl02="" if stateabv ~=zip_state
gen sldl_id = sldl02
rename afact afact_lower
rename afact2 afact2_lower

* merge with SLER data in 2005 and 2008
merge m:1 year sldl_id stateabv using sler_l_2005.dta , gen(mgsldl2005)
drop if mgsldl2005==2
merge m:1 year sldl_id stateabv using sler_l_2008.dta , gen(mgsldl2008) update


* merge with district-level entropy data
replace fipsst = "0" + fipsst if strlen(fipsst)==1
replace sldl02 = "0" + sldl02 if strlen(sldl02)==2
replace sldl02 = "00" + sldl02 if strlen(sldl02)==1

gen geoid_l = "62000US" + fipsst + sldl02
merge m:1 geoid_l using entropy_l.dta, gen(mgsldl)


* Now merge with upper-level district identifiers
joinby zcta5 using zip_sldu.dta, unmatched(master) _merge(mgzip_sldu)
tab opst
replace sldu02="" if stateabv ~=zip_state
rename afact afact_upper
rename afact2 afact2_upper
gen sldu_id = sldu02
* merge with upper-level SLER data for 2005 and 2008
merge m:1 year sldu_id stateabv using sler_u_2005.dta , gen(mgsldu2005)
drop if mgsldu2005==2
merge m:1 year sldu_id stateabv using sler_u_2008.dta , gen(mgsldu2008) update



replace sldu02 = "0" + sldu02 if strlen(sldu02)==2
replace sldu02 = "00" + sldu02 if strlen(sldu02)==1

gen geoid_u = "61000US" + fipsst + sldu02

*merge upper-district entropy scores
merge m:1 geoid_u using entropy_u.dta, gen(mgsldu)

* merge county, puma, and state context vars:
merge m:1 geoid_cnty using entropy_cnty.dta, gen(mgcnty)
drop if mgcnty==2
merge m:1 geoid_puma using entropy_puma.dta, gen(mgpuma)
drop if mgpuma==2
merge m:1 stateabv using entropy_st.dta, gen(mgst)
merge m:1 fips using personal.dta, nogen

*V21 contains state legislative party codes. This section of code 
*	creates dummy variables for whether the respondent is represented 
*	by a member of her party at the lower and upper chambers (winner_low and winner_up).

recode V21 (100=1)(200=-1)(400=0), gen(rep_party)
recode V21_upper (100=1)(200=-1)(400=0), gen(rep_party_upper)

gen winner_low = 1 if V21==100 & party3==1
replace winner_low = 1 if V21==200 & party3==-1
replace winner_low=0 if party3==0
replace winner_low=0 if V21==100 & party3~=1
replace winner_low=0 if V21==200 & party3~=-1
replace winner_low=total_rep/(total_rep + total_dem) if party3==-1 & total_rep~=. & total_dem~=.
replace winner_low=total_dem/(total_rep + total_dem) if party3==1 & total_rep~=. & total_dem~=.
replace winner_low=total_repNHVT/(total_repNHVT + total_demNHVT) if party3==-1 & total_repNHVT~=. & total_demNHVT~=.
replace winner_low=total_demNHVT/(total_repNHVT + total_demNHVT) if party3==1 & total_repNHVT~=. & total_demNHVT~=.

gen winner_up = 1 if V21_upper==100 & party3==1
replace winner_up = 1 if V21_upper==200 & party3==-1
replace winner_up=0 if party3==0
replace winner_up=0 if V21_upper==100 & party3~=1
replace winner_up=0 if V21_upper==200 & party3~=-1
replace winner_low=total_rep_upper/(total_rep_upper + total_dem_upper) if party3==-1 & total_rep_upper~=. & total_dem_upper~=.
replace winner_low=total_dem_upper/(total_rep_upper + total_dem_upper) if party3==1 & total_rep_upper~=. & total_dem_upper~=.
replace winner_low=total_repNHVT_upper/(total_repNHVT_upper + total_demNHVT_upper) if party3==-1 & total_repNHVT_upper~=. & total_demNHVT_upper~=.
replace winner_low=total_demNHVT_upper/(total_repNHVT_upper + total_demNHVT_upper) if party3==1 & total_repNHVT_upper~=. & total_demNHVT_upper~=.

* Create differenced vars to compare Theil's H index for lower chamber disticts
*		with those at the county level (geographic segregation) and comparing
*		county diversity with state legislive district diversity.

foreach var in race faminc age ed fb {
gen diff_`var'_H = H_`var'_ct - H_`var'_l
gen diff_`var'_e = entropy_`var'_cnty - entropy_`var'_l
}

* Estimate models and create Tables OA9-12

global base = "opfed unemployment debt_net_pc_real  mds1 mds2_abs h_diffs r_male c.r_age##c.r_age r_educ r_hisp r_black c.hou_majority##c.party3 c.policy##c.party3 c.unified_gov##c.party3 i.year"

ologit opst lncs winner_low winner_up  ///
		$base [pw=weight] if entropy_race_l~=. , cluster(fips)
	est store base
ologit opst personal_st winner_low winner_up  ///
		$base [pw=weight] if entropy_race_l~=. , cluster(fips)
	est store personal

*republicans	
ologit opst lncs winner_low winner_up  ///
		$base [pw=weight] if party3==-1 & entropy_race_l~=. , cluster(fips)
	est store base_gop
ologit opst personal_st winner_low winner_up  ///
		$base [pw=weight] if party3==-1 & entropy_race_l~=. , cluster(fips)
	est store personal_gop	
*democrats	
ologit opst lncs winner_low winner_up  ///
		$base [pw=weight] if party3==1 & entropy_race_l~=., cluster(fips)
	est store base_dem
ologit opst personal_st winner_low winner_up  ///
		$base [pw=weight] if party3==1 & entropy_race_l~=. , cluster(fips)
	est store personal_dem
*independents	
ologit opst lncs winner_low winner_up  ///
		$base [pw=weight] if party3==0 & entropy_race_l~=., cluster(fips)
	est store base_ind
ologit opst personal_st winner_low winner_up  ///
		$base [pw=weight] if party3==0 & entropy_race_l~=., cluster(fips)
	est store personal_ind	
	
foreach var in race faminc age ed fb {

global list`var'1  "diff_`var'_H entropy_`var'_l" 
global list`var'2  "stdH_`var'_ct stdH_`var'_l entropy_`var'_l"

foreach model in 1 2 {
ologit opst ${list`var'`model'} winner_low winner_up  ///
		$base [pw=weight] , cluster(fips)
	est store list`var'`model'	
	}
foreach model in 1 2 {
ologit opst ${list`var'`model'} winner_low winner_up  ///
		$base [pw=weight] if party3==-1 , cluster(fips)
	est store list`var'`model'_gop
	}	
foreach model in 1 2 {
ologit opst ${list`var'`model'} winner_low winner_up ///
		$base [pw=weight] if party3==0 , cluster(fips)
	est store list`var'`model'_ind	
	}	
foreach model in 1 2 {
ologit opst ${list`var'`model'} winner_low winner_up  ///
		$base [pw=weight] if party3==1, cluster(fips)
	est store list`var'`model'_dem
	}	
} 

esttab base listrace2 listfaminc2 listage2 listed2 listfb2 using tableOA9_12.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N aic bic) style(tex) ///
	label nolz ///
	keep(lncs stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) ///
	order(lncs stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) replace

esttab base_gop listrace2_gop listfaminc2_gop listage2_gop listed2_gop listfb2_gop using tableOA9_12.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N aic bic) style(tex) ///
	label nolz 	/// 
	keep(lncs  stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) ///
	order(lncs stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) append

esttab base_ind listrace2_ind listfaminc2_ind listage2_ind listed2_ind listfb2_ind using tableOA9_12.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N aic bic) style(tex) ///
	label nolz /// 
	keep(lncs  stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) ///
	order(lncs  stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) append
	
esttab base_dem listrace2_dem listfaminc2_dem listage2_dem listed2_dem listfb2_dem using tableOA9_12.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N aic bic) style(tex) ///
	label nolz /// 
	keep(lncs  stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) ///
	order(lncs  stdH_*_ct stdH_*_l entropy_*_l winner_low winner_up) append


	
	
*find state means for predictions
bysort fips year: gen num=_n
replace num=. if year~=2005 & year~=2008
foreach var in unemployment debt_net_pc_real mds1 mds2_abs h_diffs ///
	hou_majority policy ///
	stdH_age_ct stdH_age_l stdH_age_u ///
	stdH_race_ct stdH_race_l stdH_race_u ///
	stdH_ed_ct stdH_ed_l stdH_ed_u /// 
	stdH_faminc_ct stdH_faminc_l stdH_faminc_u /// 
	stdH_fb_ct stdH_fb_l stdH_fb_u {
	quietly sum `var' if num==1, detail
	global at`var' = r(mean)
}

*find individual var means from base model
est restore base
foreach var in opfed r_male r_age r_educ r_hisp r_black ///
	party3 winner_low winner_up ///
	entropy_age_puma entropy_age_l entropy_age_u ///
	entropy_race_puma entropy_race_l entropy_race_u ///
	entropy_ed_puma entropy_ed_l entropy_ed_u /// 
	entropy_faminc_puma entropy_faminc_l entropy_faminc_u /// 
	entropy_fb_puma entropy_fb_l entropy_fb_u  {
	quietly sum `var' if e(sample), detail
	global at`var' = r(mean)
}	

* Calculate marginal effects for first difference plot (Figure OA3)

foreach var in race faminc age ed fb {
gen est_H_`var' = .
gen se_H_`var' = .
gen lo90_H_`var' = .
gen up90_H_`var' = .
gen est_e_`var' = .
gen se_e_`var' = .
gen lo90_e_`var' = .
gen up90_e_`var' = .

est restore list`var'2

margins, expression(predict(pr outcome(3)) + predict(pr outcome(4))) ///
	at(stdH_`var'_ct = 0 stdH_`var'_l = (-.5 .5) entropy_`var'_l =${atentropy_`var'_l} ///
	unemployment = $atunemployment debt_net_pc_real = $atdebt_net_pc_real /// 
	mds1 = $atmds1 mds2_abs = $atmds2_abs ///
	h_diffs = $ath_diffs hou_majority = $athou_majority policy = $atpolicy ///
	opfed = $atopfed r_male = $atr_male r_age = $atr_age r_educ = $atr_educ ///
	r_hisp = $atr_hisp r_black = $atr_black ///
	party3 = $atparty3 ///
	  winner_low =$atwinner_low winner_up=$atwinner_up unified_gov=0) ///
	pwcompare level(90) 	
mat a=r(table_vs)
replace est_H_`var' = a[1,1] in 2
replace se_H_`var' = a[2,1] in 2
replace lo90_H_`var' = a[5,1] in 2
replace up90_H_`var' = a[6,1] in 2

}

foreach var in race faminc age ed fb {
foreach c in gop ind dem {
gen est_H_`var'_`c' = .
gen se_H_`var'_`c' = .
gen lo90_H_`var'_`c' = .
gen up90_H_`var'_`c' = .
gen est_e_`var'_`c' = .
gen se_e_`var'_`c' = .
gen lo90_e_`var'_`c' = .
gen up90_e_`var'_`c' = .
}

foreach c in gop dem {

est restore list`var'2_`c'

margins, expression(predict(pr outcome(3)) + predict(pr outcome(4))) ///
	at(stdH_`var'_ct = 0 stdH_`var'_l = (-.5 .5) entropy_`var'_l =${atentropy_`var'_l} ///
	unemployment = $atunemployment debt_net_pc_real = $atdebt_net_pc_real /// 
	mds1 = $atmds1 mds2_abs = $atmds2_abs ///
	h_diffs = $ath_diffs hou_majority = $athou_majority policy = $atpolicy ///
	opfed = $atopfed r_male = $atr_male r_age = $atr_age r_educ = $atr_educ ///
	r_hisp = $atr_hisp r_black = $atr_black ///
	  winner_low =$atwinner_low winner_up=$atwinner_up unified_gov=0) ///
	pwcompare level(90) 	
mat a=r(table_vs)
replace est_H_`var'_`c' = a[1,1] in 2
replace se_H_`var'_`c' = a[2,1] in 2
replace lo90_H_`var'_`c' = a[5,1] in 2
replace up90_H_`var'_`c' = a[6,1] in 2

}

foreach c in ind {

est restore list`var'2_`c'

margins, expression(predict(pr outcome(3)) + predict(pr outcome(4))) ///
	at(stdH_`var'_ct = 0 stdH_`var'_l = (-.5 .5) entropy_`var'_l =${atentropy_`var'_l}  ///
	unemployment = $atunemployment debt_net_pc_real = $atdebt_net_pc_real /// 
	mds1 = $atmds1 mds2_abs = $atmds2_abs ///
	h_diffs = $ath_diffs hou_majority = $athou_majority policy = $atpolicy ///
	opfed = $atopfed r_male = $atr_male r_age = $atr_age r_educ = $atr_educ ///
	r_hisp = $atr_hisp r_black = $atr_black unified_gov=0) ///
	pwcompare level(90) 	
mat a=r(table_vs)
replace est_H_`var'_`c' = a[1,1] in 2
replace se_H_`var'_`c' = a[2,1] in 2
replace lo90_H_`var'_`c' = a[5,1] in 2
replace up90_H_`var'_`c' = a[6,1] in 2

}
}

local counter = 0
foreach var in race faminc ed age fb {
capture drop n`var'*
gen n`var'=`counter' + 1
gen n`var'_gop = n`var' + 1
gen n`var'_ind = n`var' + 2
gen n`var'_dem = n`var' + 3
local counter= `counter' + 6
}

gen model=.
replace model=1 in 1
replace model=2 in 2

*Create Fig OA3
twoway scatter est_H_race nrace if model==2, msize(small) mcolor(black) || ///
	rspike lo90_H_race up90_H_race nrace if model==2, lcolor(black) || ///
	scatter est_H_race_gop nrace_gop if model==2, msize(small) mcolor(maroon) msymbol(triangle) || ///
	rspike lo90_H_race_gop up90_H_race_gop nrace_gop if model==2, lcolor(maroon) || ///
	scatter est_H_race_ind nrace_ind if model==2, msize(small) mcolor(gs8) msymbol(diamond) || ///
	rspike lo90_H_race_ind up90_H_race_ind nrace_ind if model==2, lcolor(gs8) || ///
	scatter est_H_race_dem nrace_dem if model==2, msize(small) mcolor(navy) msymbol(square) || ///
	rspike lo90_H_race_dem up90_H_race_dem nrace_dem if model==2, lcolor(navy) || ///
	scatter est_H_faminc nfaminc if model==2, msize(small) mcolor(black) || ///
	rspike lo90_H_faminc up90_H_faminc nfaminc if model==2, lcolor(black) || ///
	scatter est_H_faminc_gop nfaminc_gop if model==2, msize(small) mcolor(maroon) msymbol(triangle) || ///
	rspike lo90_H_faminc_gop up90_H_faminc_gop nfaminc_gop if model==2, lcolor(maroon) || ///
	scatter est_H_faminc_ind nfaminc_ind if model==2, msize(small) mcolor(gs8) msymbol(diamond) || ///
	rspike lo90_H_faminc_ind up90_H_faminc_ind nfaminc_ind if model==2, lcolor(gs8) || ///
	scatter est_H_faminc_dem nfaminc_dem if model==2, msize(small) mcolor(navy) msymbol(square) || ///
	rspike lo90_H_faminc_dem up90_H_faminc_dem nfaminc_dem if model==2, lcolor(navy) || ///
	scatter est_H_ed ned if model==2, msize(small) mcolor(black) || ///
	rspike lo90_H_ed up90_H_ed ned if model==2, lcolor(black) || ///
	scatter est_H_ed_gop ned_gop if model==2, msize(small) mcolor(maroon) msymbol(triangle) || ///
	rspike lo90_H_ed_gop up90_H_ed_gop ned_gop if model==2, lcolor(maroon) || ///
	scatter est_H_ed_ind ned_ind if model==2, msize(small) mcolor(gs8) msymbol(diamond) || ///
	rspike lo90_H_ed_ind up90_H_ed_ind ned_ind if model==2, lcolor(gs8) || ///
	scatter est_H_ed_dem ned_dem if model==2, msize(small) mcolor(navy) msymbol(square) || ///
	rspike lo90_H_ed_dem up90_H_ed_dem ned_dem if model==2, lcolor(navy) || ///
	scatter est_H_age nage if model==2, msize(small) mcolor(black) || ///
	rspike lo90_H_age up90_H_age nage if model==2, lcolor(black) || ///
	scatter est_H_age_gop nage_gop if model==2, msize(small) mcolor(maroon) msymbol(triangle) || ///
	rspike lo90_H_age_gop up90_H_age_gop nage_gop if model==2, lcolor(maroon) || ///
	scatter est_H_age_ind nage_ind if model==2, msize(small) mcolor(gs8) msymbol(diamond) || ///
	rspike lo90_H_age_ind up90_H_age_ind nage_ind if model==2, lcolor(gs8) || ///
	scatter est_H_age_dem nage_dem if model==2, msize(small) mcolor(navy) msymbol(square) || ///
	rspike lo90_H_age_dem up90_H_age_dem nage_dem if model==2, lcolor(navy) || ///
	scatter est_H_fb nfb if model==2, msize(small) mcolor(black) || ///
	rspike lo90_H_fb up90_H_fb nfb if model==2, lcolor(black) || ///
	scatter est_H_fb_gop nfb_gop if model==2, msize(small) mcolor(maroon) msymbol(triangle) || ///
	rspike lo90_H_fb_gop up90_H_fb_gop nfb_gop if model==2, lcolor(maroon) || ///
	scatter est_H_fb_ind nfb_ind if model==2, msize(small) mcolor(gs8) msymbol(diamond) || ///
	rspike lo90_H_fb_ind up90_H_fb_ind nfb_ind if model==2, lcolor(gs8) || ///
	scatter est_H_fb_dem nfb_dem if model==2, msize(small) mcolor(navy) msymbol(square) || ///
	rspike lo90_H_fb_dem up90_H_fb_dem nfb_dem if model==2, lcolor(navy) ///
	ysize(1.5) xsize(4) legend(order( 1 3 5 7 ) label(1 "All") ///
	label(3 "Rep") label(5 "Ind") label(7 "Dem") rows(1) region(lcolor(white))) ///
	plotregion(color(white)) ///
	graphregion(color(white))  ///
	xlabel(2.5 "Race" 8.5 "Income" 14.5 "Education" 20.5 "Age" 26.5 "Foreign Born") ///
	ytitle("Change in Prob. of Favorable Opinion ({it:y{subscript:i} {&gt} 2}") ///
	ylabel(-.2(.1).3, angle(0) labgap(1.25) labsize(small) ///
	glcolor(gs14) glwidth(vthin) gextend gmax gmin) yline(0, lcolor(black))

graph export H_firstdiff.pdf, replace



	